import uuid

from alipay import AliPay
from django.template import RequestContext
from django.contrib.auth.hashers import make_password, check_password
from django.core.cache import cache
from django.core.mail import send_mail
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect

# Create your views here.
from django.template import loader
from django.urls import reverse

from AXF.settings import ALIPAY_APPID, APP_PRIVATE_KEY, ALIPAY_PUBLIC_KEY, MEDIA_ROOT_PREFIX
from App.all_config import all_types, defult, price_up, price_down, sale_up, sale_down, http_user_exist, http_ok, \
    ORDER_STATUS_NOT_PAY, ORDER_STATUS_NOT_RECEIVE, ORDER_STATUS_NOT_SEND
from App.models import MainWheel, MainNav, MainMustBuy, MainShop, MainShow, FoodType, Goods, AXFUser, Cart, Order, \
    OrderGoods
from App.views_helper import hash_str, sendmail, get_total_price


def home(request):
    main_wheels = MainWheel.objects.all()
    main_navs = MainNav.objects.all()
    main_mustbuys = MainMustBuy.objects.all()
    main_shops = MainShop.objects.all()
    main_shop0_1 = main_shops.first()
    main_shop1_3 = main_shops[1:3]
    main_shop3_7 = main_shops[3:7]
    main_shop7_11 = main_shops[7:11]
    main_shows = MainShow.objects.all()

    data = {
        "title": "首页",
        "main_wheels": main_wheels,
        "main_navs": main_navs,
        "main_mustbuys": main_mustbuys,
        "main_shop0_1": main_shop0_1,
        "main_shop1_3": main_shop1_3,
        "main_shop3_7": main_shop3_7,
        "main_shop7_11": main_shop7_11,
        "main_shows": main_shows,

    }

    return render(request, 'main/home.html', context=data)


def market(request):
    return redirect(reverse('app:market_with_params', kwargs={
        "typeid": 104749,
        "childcid": 0,
        'order_rule': 0,
    }))


def market_with_params(request, typeid, childcid, order_rule):
    foodtypes = FoodType.objects.all()
    goods_list = Goods.objects.filter(categoryid=typeid)
    if childcid == all_types:
        pass
    else:
        goods_list = goods_list.filter(childcid=childcid)
    if order_rule == defult:
        pass
    elif order_rule == price_up:
        goods_list = goods_list.order_by("price")
    elif order_rule == price_down:
        goods_list = goods_list.order_by("-price")
    elif order_rule == sale_down:
        goods_list = goods_list.order_by("-productnum")
    elif order_rule == sale_up:
        goods_list = goods_list.order_by("productnum")

    foodtype = foodtypes.get(typeid=typeid)
    """
    全部分类:0#进口水果:103534#国产水果:103533
    先根据#切割,再根据:处理
    """
    foodtypechildnames = foodtype.childtypenames
    foodtypechildnames_list = foodtypechildnames.split("#")

    foodtype_childnames_list = []
    for foodtypechildnames in foodtypechildnames_list:
        foodtype_childnames_list.append(foodtypechildnames.split(":"))

    order_rule_list = [
        ["综合排序", defult],
        ["价格降序", price_down],
        ["价格升序", price_up],
        ["销量升序", sale_up],
        ["销量降序", sale_down],

    ]
    data = {

        "title": "闪购",
        "foodtypes": foodtypes,
        "goods_list": goods_list,
        "typeid": int(typeid),
        "childcid": childcid,
        "order_rule_list": order_rule_list,
        "order_rule_view ": order_rule,
        "foodtype_childnames_list": foodtype_childnames_list,
    }
    return render(request, 'main/market.html', context=data)


def cart(request):
    carts = Cart.objects.filter(c_user=request.user)
    is_all_select = not carts.filter(c_is_select=False).exists()
    data = {
        "title": "购物车",
        'carts': carts,
        'is_all_select': is_all_select,
        'total_price': get_total_price(),
    }
    return render(request, 'main/cart.html', context=data)


def mine(request):
    user_id = request.session.get("user_id")
    data = {
        "title": "个人中心",
        "is_login": False,
    }
    if user_id:
        user = AXFUser.objects.get(pk=user_id)
        data["is_login"] = True
        data["username"] = user.u_username
        data["icon"] = MEDIA_ROOT_PREFIX + user.u_icon.url
        data["order_not_pay"] = Order.objects.filter(o_user=user).filter(o_status=ORDER_STATUS_NOT_PAY).count()
        data['order_not_receive'] = Order.objects.filter(o_user=user).filter(
            o_status__in=[ORDER_STATUS_NOT_RECEIVE, ORDER_STATUS_NOT_SEND]).count()
        data['order_not_receive'] = Order.objects.filter(o_user=user).filter(
            o_status__in=[ORDER_STATUS_NOT_RECEIVE, ORDER_STATUS_NOT_SEND]).count()
    return render(request, 'main/mine.html', context=data)


def register(request):
    if request.method == 'GET':
        data = {
            "title": "注册",
        }
        return render(request, 'user/register.html', context=data)

    elif request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        email = request.POST.get("email")
        icon = request.FILES.get("icon")

        # password = hash_str(password)
        password = make_password(password)

        user = AXFUser()
        user.u_username = username
        user.u_password = password
        user.u_email = email
        user.u_icon = icon
        user.save()
        u_token = uuid.uuid4().hex
        cache.set(u_token, user.id, timeout=60 * 60)

        sendmail(username, email, u_token)
        return redirect(reverse('app:login'))


def login(request):
    if request.method == 'GET':
        error_message = request.session.get('error_message')

        data = {

            "title": "登录"
        }

        if error_message:
            del request.session['error_message']
            data['error_message'] = error_message

        return render(request, 'user/login.html', context=data)

    elif request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password")
        print(password)

        users = AXFUser.objects.filter(u_username=username)
        if users.exists():
            user = users.first()
            if check_password(password, user.u_password):
                if user.is_active:
                    request.session['user_id'] = user.id
                    return redirect(reverse('app:mine'))
                else:
                    print("用户未激活")
                    request.session['error_message'] = 'not active'
                    redirect(reverse('app:login'))
            else:
                print("密码错误")
                request.session['error_message'] = 'password error'
                return redirect(reverse('app:login'))
        print("用户不存在")
        request.session['error_message'] = 'user does not exist'
        return redirect(reverse('app:login'))


def checkuser(request):
    username = request.GET.get("username")
    users = AXFUser.objects.filter(u_username=username)
    data = {
        "status": http_ok,
        "msg": "用户名可用",
    }
    if users.exists():
        data['status'] = http_user_exist
        data['msg'] = "用户名已存在"
    else:
        pass

    return JsonResponse(data=data)


def logout(request):
    request.session.flush()
    return redirect(reverse('app:mine'))


def active(request):
    u_token = request.GET.get('u_token')
    user_id = cache.get(u_token)
    if user_id:
        cache.delete(u_token)
        user = AXFUser.objects.get(pk=user_id)
        user.is_active = True
        user.save()
        return redirect(reverse('app:login'))

    return render(request, 'user/activate_fail.html')


def addtocart(request):
    goodsid = request.GET.get('goodsid')

    carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid)

    if carts.exists():
        cart_obj = carts.first()
        cart_obj.c_goods_num = cart_obj.c_goods_num + 1
    else:
        cart_obj = Cart()
        cart_obj.c_goods_id = goodsid
        cart_obj.c_user = request.user

    cart_obj.save()

    data = {
        'status': 200,
        'msg': 'add success',
        'c_goods_num': cart_obj.c_goods_num
    }

    return JsonResponse(data=data)


def changecartstate(request):
    cart_id = request.GET.get('cartid')
    cart_obj = Cart.objects.get(pk=cart_id)
    cart_obj.c_is_select = not cart_obj.c_is_select
    cart_obj.save()
    is_all_select = not Cart.objects.filter(c_user=request.user).filter(c_is_select=False).exists()
    data = {
        'status': 200,
        'msg': 'change ok',
        'c_is_select': cart_obj.c_is_select,
        'is_all_select': is_all_select,
        'total_price': get_total_price(),
    }

    return JsonResponse(data=data)


def subshopping(request):
    cart_id = request.GET.get('cartid')
    cart_obj = Cart.objects.get(pk=cart_id)

    data = {
        'status': 200,
        'msg': 'ok',

    }
    if cart_obj.c_goods_num > 1:
        cart_obj.c_goods_num = cart_obj.c_goods_num - 1
        data['c_goods_num'] = cart_obj.c_goods_num
        data["total_price"] = get_total_price()
    else:
        cart_obj.delete()
        data['c_goods_num'] = 0
        data["total_price"] = get_total_price()

    return JsonResponse(data=data)


def allselect(request):
    cart_list = request.GET.get('cart_list')

    cart_list = cart_list.split("#")

    carts = Cart.objects.filter(id__in=cart_list)

    for cart_obj in carts:
        cart_obj.c_is_select = not cart_obj.c_is_select
        cart_obj.save()

    data = {
        'status': 200,
        'msg': 'ok',
        'total_price': get_total_price()
    }

    return JsonResponse(data=data)


def makeorder(request):
    carts = Cart.objects.filter(c_user=request.user).filter(c_is_select=True)

    order = Order()

    order.o_user = request.user

    order.o_price = get_total_price()

    order.save()

    for cart_obj in carts:
        ordergoods = OrderGoods()
        ordergoods.o_order = order
        ordergoods.o_goods_num = cart_obj.c_goods_num
        ordergoods.o_goods = cart_obj.c_goods
        ordergoods.save()
        cart_obj.delete()

    data = {
        "status": 200,
        "msg": 'ok',
        'order_id': order.id
    }

    return JsonResponse(data)


def orderdetail(request):
    order_id = request.GET.get("orderid")
    order = Order.objects.get(pk=order_id)
    data = {
        'title': "订单详情",
        'order': order,

    }
    return render(request, 'order/order_detail.html', context=data)


def payed(request):
    order_id = request.GET.get('orderid')
    order = Order.objects.get(pk=order_id)
    order.o_status = ORDER_STATUS_NOT_SEND
    order.save()
    data = {
        "status": 200,
        "msg": 'payed ok',
    }
    return JsonResponse(data=data)


def orderlistnotpay(request):
    orders = Order.objects.filter(o_user=request.user).filter(o_status=ORDER_STATUS_NOT_PAY)
    data = {
        'title': '订单列表',
        'orders': orders,
    }
    return render(request, 'order/order_list_not_pay.html', context=data)


def alipay(request):
    # 构建支付的客户端,AlipayClient
    alipay = AliPay(
        appid=ALIPAY_APPID,
        app_notify_url=None,  # 默认回调url
        app_private_key_string=APP_PRIVATE_KEY,
        # 支付宝的公钥，验证支付宝回传消息使用，不是你自己的公钥,
        alipay_public_key_string=ALIPAY_PUBLIC_KEY,
        sign_type="RSA2",  # RSA 或者 RSA2
        debug=False  # 默认False
    )
    # 使用Alipay进行支付请求的发起
    subject = 'amd线程撕裂者3950+2080ti'
    # 电脑网站支付，需要跳转到https://openapi.alipay.com/gateway.do? + order_string
    order_string = alipay.api_alipay_trade_page_pay(
        out_trade_no="20191212",
        total_amount=20000,
        subject=subject,
        return_url="https://www.baidu.com",
        notify_url="https://www.baidu.com"  # 可选, 不填则使用默认notify url
    )

    # 客户端操作
    return redirect('https://openapi.alipaydev.com/gateway.do?' + order_string)


def index(request):
    return redirect(reverse('app:home'))
